<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\widget;


use think\facade\View;
use think\template\TagLib;

/**
 * 城市标签组件
 * @author 牧羊人
 * @since 2020/7/3
 * Class City
 * @package app\admin\taglib
 */
class City extends TagLib
{
    // 自定义标签列表
    protected $tags = [
        'select' => ['attr' => 'name', 'close' => 0],
    ];

    /**
     * 城市选择组件
     * @param $tag 标签参数
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @since 2020/7/3
     * @author 牧羊人
     */
    public function tagSelect($tag)
    {
        // 城市ID
        $value = $tag['value'];
        // 参数值拆分
        $item = explode(',', $value);
        // 层级
        $limit = isset($tag['limit']) ? intval($tag['limit']) : 1;
        $cityMod = new \app\admin\model\City();
        $cityList = $cityMod->getChilds(0);

        // 省份信息
        // 省份ID
        $provinceId = 'isset($info[\'province_id\'])?$info[\'province_id\']: 0';
        $parse = "<div class=\"layui-input-inline\" style=\"width:165px;\">";
        $parse .= "<select name=\"province_id\" id=\"province_id\" lay-filter=\"province_id\" lay-search=\"\">";
        $parse .= "<option value=\"\">【请选择省份】</option>";
        foreach ($cityList as $key => $val) {
            $parse .= '<option value="' . $val["id"] . '" <?php echo (' . $provinceId . ')=="' . $val["id"] . '" ? "selected=\"\"" : null ?> >' . $val["name"] . '</option>';
        }
        $parse .= "</select>";
        $parse .= "</div>";

        // 城市信息
        $cityId = 'isset($info[\'city_id\'])?$info[\'city_id\']: 0';
        $parse .= "<div class=\"layui-input-inline\" style=\"width:165px;\">";
        $parse .= "<select name=\"city_id\" id=\"city_id\" lay-filter=\"city_id\" lay-search=\"\">";
        $parse .= "<option value=\"<?php echo $cityId?>\"><?php echo " . $item[1] . "?></option>";
        $parse .= "</select>";
        $parse .= "</div>";

        // 县区信息
        $districtArr = explode(",", $item[2]);
        // 县区ID
        $districtId = 'isset($info[\'district_id\'])?$info[\'district_id\']:0';
        $parse .= "<div class=\"layui-input-inline\" style=\"width:165px;\">";
        $parse .= "<select name=\"district_id\" id=\"district_id\" lay-filter=\"district_id\" lay-search=\"\">";
        $parse .= "<option value=\"<?php echo $districtId?>\"><?php echo " . $item[2] . "?></option>";
        $parse .= "</select>";
        $parse .= "</div>";

        $parse .= "</br>";
        $parse .= '<script type="text/javascript">
layui.use([\'form\'],function(){

	// 声明变量
	var form = layui.form,
		$ = layui.$;

	// 选择省份
	form.on(\'select(province_id)\',function(data){
		var id = data.value;
		console.log("省份ID:"+id);
		var select = data.othis;
		if (select[0]) {
			if (id > 0) {
				$.post("/city/getChilds", { \'id\':id }, function(data){
					if (data.success) {
						var str = "";
						$.each(data.data, function(i,item){
							str += "<option value=\"" + i + "\" >" + item.name + "</option>";
						});
						$("#city_id").html(\'<option value="">【请选择市】</option>\' + str);
						$("#district_id").html(\'<option value="">【请选择县/区】</option>\');
						form.render(\'select\');
					}else{
						layer.msg(data.msg,{ icon: 5 });
						
						$("#city_id").html(\'<option value="">【请选择市】</option>\');
						$("#district_id").html(\'<option value="">【请选择县/区】</option>\');
						form.render(\'select\');
						
						
						return false;
					}
				}, \'json\');
			} else {
				
			}
		}
	});
	
	// 选择城市
	form.on(\'select(city_id)\',function(data){
		var id = data.value;
		console.log("城市ID:"+id);
		var select = data.othis;
		if (select[0]) {
			if (id > 0) {
				$.post("/city/getChilds", { \'id\':id }, function(data){
					if (data.success) {
						var str = "";
						$.each(data.data, function(i,item){
							str += "<option value=\"" + i + "\" >" + item.name + "</option>";
						});
						$("#district_id").html(\'<option value="">【请选择县/区】</option>\' + str);
						form.render(\'select\');
					}
				}, \'json\');
			} else {
				layer.msg(data.msg,{ icon: 5 });
				
				$("#district_id").html(\'<option value="">【请选择县/区】</option>\');
				form.render(\'select\');

				return false;
			}
		}
	});
	
	// 选择县区
	form.on("select(district_id)",function(data){
		var id = data.value;
		console.log("县区ID:"+id);
	});
	
});
</script>';

        return $parse;
    }
}